package com.sxm.yiwei.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.CircleTransform;
import com.sxm.yiwei.utils.InternetAddressUtis;


import org.xutils.common.Callback;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;
import java.util.Map;

/**
 * Created by KuKa on 2017/6/2.
 * 心语心愿列表
 */

public class CryHopelistAdapter extends BaseAdapter {
    private Context context;
    private List<Map<String, String>> list;
    private OnClickListenerItem onClickListenerItem;
    private String type;

    public CryHopelistAdapter(Context context, List<Map<String, String>> list) {
        this.context=context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view ;
        ViewHolder holder;
        if (convertView==null){
            view = View.inflate(context, R.layout.cryhope_item, null);
            holder = new ViewHolder();
            holder.texttype = (TextView) view.findViewById(R.id.cry_texttype);
            holder.textwish = (TextView) view.findViewById(R.id.cry_textwish);
            holder.userimg = (ImageView) view.findViewById(R.id.hope_userimg);
            holder.wishimg = (ImageView) view.findViewById(R.id.hope_wishimg);
            holder.hopemoney = (Button) view.findViewById(R.id.cry_hopemoney);
            holder.hopetalk = (Button) view.findViewById(R.id.cry_hopetalk);
            view.setTag(holder);
        }else{
            view=convertView;
            holder = (ViewHolder) view.getTag();
        }

        holder.textwish.setText(list.get(position).get("wish"));
        type = list.get(position).get("type");

        if (type.equals("1")){
            holder.texttype.setText("手");
        }else if (type.equals("2")){
            holder.texttype.setText("均");
        }else if(type.equals("3")){
            holder.texttype.setText("一");
        }else if (type.equals("4")){
            holder.texttype.setText("二");
        }else if (type.equals("5")){
            holder.texttype.setText("定");
        }

        Picasso.with(context).resumeTag("a");
        Picasso.with(context).setIndicatorsEnabled(true);
        x.image().bind(holder.wishimg,InternetAddressUtis.IMG_URL+list.get(position).get("imageUrl"));
       // ImageOptions options = new ImageOptions.Builder().setCircular(true).build();
      //  x.image().bind(holder.userimg,InternetAddressUtis.IMG_URL+list.get(position).get("avatar"),options);
     //   x.image().bind(holder.userimg,InternetAddressUtis.IMG_URL+list.get(position).get("avatar"));
        Picasso.with(context).load(InternetAddressUtis.IMG_URL+list.get(position).get("avatar")).transform(new CircleTransform()).into(holder.userimg);
        notifyDataSetChanged();
        // 设置点击事件
        holder.hopemoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListenerItem!=null){
                    onClickListenerItem.OnClickListenerButton(position);
                }
            }
        });
        holder.wishimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListenerItem!=null){
                    onClickListenerItem.OnClickListenerImg(position);
                }
            }
        });

        return view;

    }
    public class ViewHolder{
        ImageView userimg,wishimg;
        TextView textwish,texttype;
        Button hopemoney,hopetalk;
    }
    public interface OnClickListenerItem{
        void OnClickListenerImg(int position);
        void OnClickListenerButton(int position);
    }
    public void setOnClickListenerItem(OnClickListenerItem onClickListenerItem1){
        this.onClickListenerItem = onClickListenerItem1;
    }
}
